using UnityEngine;
using UnityEngine.UI;

public class UIManager_1 : MonoBehaviour
{
    public static UIManager_1 Instance;

    [Header("UI面板")]
    public GameObject gameAreaPanel;    // 游戏区域面板
    public GameObject gameUIPanel;      // 游戏中UI面板
    public GameObject resultPanel;      // 结果面板
    public GameObject rechargePanel;    // 充值面板
    public GameObject infoPanel;        // 游戏信息面板

    [Header("文本组件")]
    public Text freePlayText;           // 免费次数文本
    public Text scoreText;              // 分数文本
    public Text dartsText;              // 飞镖数文本
    public Text timeText;               // 时间文本
    public Text resultText;             // 结果文本
    public Text coinText;               // 硬币数文本
    public Text powerValueText;         // 力量值文本
    private Text stabilityValueText;     // 稳定性值文本

    [Header("滑块组件")]
    public Slider powerSlider;          // 力量滑块
    public Slider stabilitySlider;      // 稳定性滑块

    [Header("按钮组件")]
    public Button startButton;          // 开始按钮
    public Button infoButton;           // 信息按钮
    public Button throwButton;          // 投掷按钮
    public Button backButton;           // 返回按钮
    public Button yesButton;            // 充值确认按钮
    public Button noButton;             // 充值取消按钮
    public Button closeButton;          // 关闭信息按钮

    private void Awake()
    {
        if (Instance == null)
            Instance = this;
        else
            Destroy(gameObject);
    }

    void Start()
    {
        // 初始化UI状态
        gameAreaPanel.SetActive(false);
        gameUIPanel.SetActive(false);
        resultPanel.SetActive(false);
        rechargePanel.SetActive(false);
        infoPanel.SetActive(false);

        // 绑定按钮事件
        startButton.onClick.AddListener(GameManager_1.Instance.StartGame);
        infoButton.onClick.AddListener(ShowInfoPanel);
        throwButton.onClick.AddListener(() => DartController.Instance.ThrowDart());
        backButton.onClick.AddListener(ReturnToMenu);
        yesButton.onClick.AddListener(GameManager_1.Instance.Recharge);
        noButton.onClick.AddListener(HideRechargePanel);
        closeButton.onClick.AddListener(HideInfoPanel);
    }

    // 显示/隐藏游戏区域面板
    public void ShowGameAreaPanel()
    {
        gameAreaPanel.SetActive(true);
        freePlayText.text = "免费次数: " + (GameManager_1.Instance.hasFreePlay ? "1" : "0");
        coinText.text = "时空币: " + GameManager_1.Instance.GetCoins();
    }

    public void HideGameAreaPanel()
    {
        gameAreaPanel.SetActive(false);
    }

    // 显示/隐藏游戏中UI
    public void ShowGameUI()
    {
        gameUIPanel.SetActive(true);
        UpdateGameUI();
    }

    public void HideGameUI()
    {
        gameUIPanel.SetActive(false);
    }

    // 显示/隐藏结果面板
    public void ShowResultPanel(bool isWin)
    {
        resultPanel.SetActive(true);
        resultText.text = isWin ? "恭喜你赢了！" : "游戏失败！";
    }

    public void HideResultPanel()
    {
        resultPanel.SetActive(false);
    }

    // 显示/隐藏充值面板
    public void ShowRechargePanel()
    {
        rechargePanel.SetActive(true);
    }

    public void HideRechargePanel()
    {
        rechargePanel.SetActive(false);
    }

    // 显示/隐藏信息面板
    public void ShowInfoPanel()
    {
        infoPanel.SetActive(true);
    }

    public void HideInfoPanel()
    {
        infoPanel.SetActive(false);
    }

    // 更新游戏UI数据
    public void UpdateGameUI()
    {
        scoreText.text = "分数: " + GameManager_1.Instance.currentScore;
        dartsText.text = "剩余飞镖: " + GameManager_1.Instance.remainingDarts;
        
        // 格式化时间为 MM:SS
        int minutes = Mathf.FloorToInt(GameManager_1.Instance.timeRemaining / 60);
        int seconds = Mathf.FloorToInt(GameManager_1.Instance.timeRemaining % 60);
        timeText.text = "剩余时间: " + minutes.ToString("00") + ":" + seconds.ToString("00");
        
        // 更新硬币数
        coinText.text = "时空币: " + GameManager_1.Instance.GetCoins();
        
        // 更新力量和稳定性显示
        powerValueText.text = DartController.Instance.currentPower.ToString("0.0");
        //stabilityValueText.text = DartController.Instance.currentStability.ToString("0.0");
        
        // 更新滑块值
        powerSlider.value = DartController.Instance.currentPower;
        stabilitySlider.value = DartController.Instance.currentStability;
    }

    // 返回菜单
    public void ReturnToMenu()
    {
        HideResultPanel();
        HideGameUI();
        GameManager_1.Instance.ResetGame();
        ShowGameAreaPanel();
    }
}